<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Register extended class used to create base node type</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="domdocument.normalizedocument.html">DOMDocument::normalizeDocument</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="domdocument.relaxngvalidate.html">DOMDocument::relaxNGValidate</a></div>
 <div class="up"><a href="class.domdocument.html">DOMDocument</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="domdocument.registernodeclass" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">DOMDocument::registerNodeClass</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.2.0)</p><p class="refpurpose"><span class="refname">DOMDocument::registerNodeClass</span> &mdash; <span class="dc-title">Register extended class used to create base node type</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-domdocument.registernodeclass-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">bool</span> <span class="methodname"><strong>DOMDocument::registerNodeClass</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$baseclass</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$extendedclass</code></span>
   )</div>


  <p class="para rdfs-comment">
   This method allows you to register your own extended DOM class to be used
   afterward by the PHP DOM extension.
  </p>
  <p class="para">
   This method is not part of the DOM standard.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-domdocument.registernodeclass-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">baseclass</code></em></dt>

     <dd>

      <p class="para">
       The DOM class that you want to extend. You can find a list of these 
       classes in the <a href="book.dom.html" class="link">chapter introduction</a>.
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">extendedclass</code></em></dt>

     <dd>

      <p class="para">
       Your extended class name. If <strong><code>NULL</code></strong> is provided, any previously 
       registered class extending <em><code class="parameter">baseclass</code></em> will
       be removed.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-domdocument.registernodeclass-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code>TRUE</code></strong>， 或者在失败时返回 <strong><code>FALSE</code></strong>。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-domdocument.registernodeclass-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.2.2</td>
       <td>
        Prior to 5.2.2, a previously registered <em><code class="parameter">extendedclass</code></em>
        had to be unregistered before being able to register a new class extending
        the same <em><code class="parameter">baseclass</code></em>.
       </td>
      </tr>


     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-domdocument.registernodeclass-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-5292">
    <p><strong>Example #1 Adding a new method to DOMElement to ease our code</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">myElement&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">DOMElement&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">appendElement</span><span style="color: #007700">(</span><span style="color: #0000BB">$name</span><span style="color: #007700">)&nbsp;{&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">appendChild</span><span style="color: #007700">(new&nbsp;</span><span style="color: #0000BB">myElement</span><span style="color: #007700">(</span><span style="color: #0000BB">$name</span><span style="color: #007700">));<br />&nbsp;&nbsp;&nbsp;}<br />}<br /><br />class&nbsp;</span><span style="color: #0000BB">myDocument&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">DOMDocument&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">setRoot</span><span style="color: #007700">(</span><span style="color: #0000BB">$name</span><span style="color: #007700">)&nbsp;{&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">appendChild</span><span style="color: #007700">(new&nbsp;</span><span style="color: #0000BB">myElement</span><span style="color: #007700">(</span><span style="color: #0000BB">$name</span><span style="color: #007700">));<br />&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">$doc&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">myDocument</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$doc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">registerNodeClass</span><span style="color: #007700">(</span><span style="color: #DD0000">'DOMElement'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'myElement'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;From&nbsp;now&nbsp;on,&nbsp;adding&nbsp;an&nbsp;element&nbsp;to&nbsp;another&nbsp;costs&nbsp;only&nbsp;one&nbsp;method&nbsp;call&nbsp;!&nbsp;<br /></span><span style="color: #0000BB">$root&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$doc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setRoot</span><span style="color: #007700">(</span><span style="color: #DD0000">'root'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$child&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$root</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">appendElement</span><span style="color: #007700">(</span><span style="color: #DD0000">'child'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$child</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setAttribute</span><span style="color: #007700">(</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'bar'</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #0000BB">$doc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">saveXML</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;root&gt;&lt;child foo=&quot;bar&quot;/&gt;&lt;/root&gt;
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-5293">
    <p><strong>Example #2 Retrieving elements as custom class</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">myElement&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">DOMElement&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">__toString</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">nodeValue</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">$doc&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DOMDocument</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$doc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">loadXML</span><span style="color: #007700">(</span><span style="color: #DD0000">"&lt;root&gt;&lt;element&gt;&lt;child&gt;text&nbsp;in&nbsp;child&lt;/child&gt;&lt;/element&gt;&lt;/root&gt;"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$doc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">registerNodeClass</span><span style="color: #007700">(</span><span style="color: #DD0000">"DOMElement"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"myElement"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$element&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$doc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getElementsByTagName</span><span style="color: #007700">(</span><span style="color: #DD0000">"child"</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">item</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">get_class</span><span style="color: #007700">(</span><span style="color: #0000BB">$element</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">//&nbsp;And&nbsp;take&nbsp;advantage&nbsp;of&nbsp;the&nbsp;__toString&nbsp;method..<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$element</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
string(9) &quot;myElement&quot;
text in child
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-5294">
    <p><strong>Example #3 Retrieving owner document</strong></p>
    <div class="example-contents"><p>
     When instantiating a custom <a href="class.domdocument.html" class="classname">DOMDocument</a> the
     <var class="varname"><var class="varname">ownerDocument</var></var> property will refer to the instantiated
     class, meaning there is no need (and in fact not possible) to use
     <span class="function"><strong>DOMDocument::registerNodeClass()</strong></span> with
     <a href="class.domdocument.html" class="classname">DOMDocument</a>
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">myDOMDocument&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">DOMDocument&nbsp;</span><span style="color: #007700">{<br />}<br /><br />class&nbsp;</span><span style="color: #0000BB">myOtherDOMDocument&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">DOMDocument&nbsp;</span><span style="color: #007700">{<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;Create&nbsp;myDOMDocument&nbsp;with&nbsp;some&nbsp;XML<br /></span><span style="color: #0000BB">$doc&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">myDOMDocument</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$doc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">loadXML</span><span style="color: #007700">(</span><span style="color: #DD0000">"&lt;root&gt;&lt;element&gt;&lt;child&gt;text&nbsp;in&nbsp;child&lt;/child&gt;&lt;/element&gt;&lt;/root&gt;"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$child&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$doc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getElementsByTagName</span><span style="color: #007700">(</span><span style="color: #DD0000">"child"</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">item</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;The&nbsp;current&nbsp;owner&nbsp;of&nbsp;the&nbsp;node&nbsp;is&nbsp;myDOMDocument<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">get_class</span><span style="color: #007700">(</span><span style="color: #0000BB">$child</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">ownerDocument</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">//&nbsp;Import&nbsp;a&nbsp;node&nbsp;from&nbsp;myDOMDocument<br /></span><span style="color: #0000BB">$newdoc&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">myOtherDOMDocument</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$child&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$newdoc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">importNode</span><span style="color: #007700">(</span><span style="color: #0000BB">$child</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;The&nbsp;new&nbsp;owner&nbsp;of&nbsp;the&nbsp;node&nbsp;has&nbsp;changed&nbsp;to&nbsp;myOtherDOMDocument<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">get_class</span><span style="color: #007700">(</span><span style="color: #0000BB">$child</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">ownerDocument</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
string(13) &quot;myDOMDocument&quot;
string(18) &quot;myOtherDOMDocument&quot;
</pre></div>
    </div>
   </div>
  </p>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="domdocument.normalizedocument.html">DOMDocument::normalizeDocument</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="domdocument.relaxngvalidate.html">DOMDocument::relaxNGValidate</a></div>
 <div class="up"><a href="class.domdocument.html">DOMDocument</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
